//业务逻辑中间件
const path = require('path');
const fileUtils = require('../utils/file_utils');
module.exports = async (ctx, next) => {
  const url = ctx.request.url;
  let filePath = url.replace('/api', '');
  filePath = '../data' + filePath + '.json';
  filePath = path.join(__dirname, filePath); //__dirname 当前文件的绝对路径
  try {
    const data = await fileUtils.getFileJsonData(filePath);
    ctx.response.body = data;
  }catch {
    const errMsg = {
      message: '读取文件失败,文件资源不存在',
      status: 404
    }
    ctx.response.body = JSON.stringify(errMsg);
  }
  await next();
} 
